package com.rocbrid.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rocbrid.common.R;
import com.rocbrid.pojo.Article;
import com.rocbrid.service.ArticleService;
import com.rocbrid.utils.ThreadLocalUtil;
import com.rocbrid.vo.req.ArticleAddVo;
import com.rocbrid.vo.req.ArticlePageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    /**
     * 新增文章
     * @param articleAddVo
     * @return
     */
    @PostMapping
    public R addArticle(@RequestBody ArticleAddVo articleAddVo){

        Map<String, Object> climas = ThreadLocalUtil.get();
        Integer id = (Integer) climas.get("id");
        articleAddVo.setCreateUser(id);
        Article article = new Article();
        BeanUtils.copyProperties(articleAddVo,article);
        articleService.save(article);
        return R.ok();
    }
    /**
     * 文章列表(条件分页)
     * @param articlePageVo
     * @return
     */
    @GetMapping
    public R getArticlePage(@RequestBody ArticlePageVo articlePageVo){
        IPage<Article> articleIPage = articleService.searchAllByCategoryIdArticleIPage(articlePageVo);
        return R.ok(articleIPage);
    }


}
